<template>
	<view class="">
		<!-- 流程头部 -->
		<process-head :process="process"></process-head>
		<slot></slot>
		<!-- 流程图片 -->
		<process-photo :process="process"></process-photo>
		<!-- 流程历史记录 -->
		<process-history :process="process"></process-history>
		<!-- 流程组件 -->
		<steps-detail :process="process"></steps-detail>
		<!-- 底部菜单栏 -->
		<process-tabbar :process="process" :checkName="checkName" :data="data" :route="route" @confirm="confirm"></process-tabbar>
	</view>
</template>

<script>
	import _custom from '@/common/_custom.js'
	import _data from '@/common/_data.js'
	import StepsDetail from './steps-detail.vue'
	import ProcessTabbar from './process-tabbar.vue'
	import ProcessHead from './process-head.vue'
	import ProcessHistory from './process-history.vue'
	import ProcessPhoto from './process-photo.vue'
	
	export default {
		components: {
			StepsDetail,
			ProcessTabbar,
			ProcessHead,
			ProcessHistory,
			ProcessPhoto
		},
		data() {
			return {
				pxid: _data.localData('userid'),
				obsusername: _data.localData('name'),
				process: {}
			}
		},
		props: {
			insflowid: {
				type: String,
				default: ''
			},
			//业务校验接口的apiname
			checkName: {
				type: String,
				default: ''
			},
			route: {
				type: String,
				default: ''
			},
			data: {
				type: [Object,Array],
				default: () => {}
			}
		},
		created() {
			this.init()
			uni.$on('process-confirm',() => {
				this.init()
			})
		},
		methods: {
			/**
			 * 获取流程
			 * status: -1,//审批状态，0：流程已完成，1：审批人，2：非审批人，3：发起人
			 */
			init() {
				this.getFlowToDoUserExist().then(r =>{
					if(this.process.FlowState == '0') {//流程未结束
						if(r) {
							this.process.status = 1;//审批人
						} else if(this.process.CreatePxid == this.pxid) {//判断创建人是否是当前用户
							this.process.status = 3;//发起人
						} else {
							this.process.status = 2;//非审批人
							if(this.process.LastPxid == this.pxid) {
								this.process.status = 6;//上一个环节是审批人
							}
						}
					} else {//流程已结束
						if(this.process.CreatePxid == this.pxid) {//判断创建人是否是当前用户
							this.process.status = 5
							if(this.process.FlowState == '1' && this.process.ForceCancelFunc != '') {//全部通过
								this.process.status = 4;//修改
							}
						} else {
							this.process.status = 0
						}
					}
					console.log(this.process)
				})
			},
			/**
			 * 获取当前待办人员是否合法并获取流程内容
			 */
			getFlowToDoUserExist() {
				let that = this;
				return new Promise((resolve,reject) => {
					_custom.getDataJava({
						apiname: 'getFlowToDoUserExist',
						apiparam: {
							insflowid: this.insflowid,
							pxid: this.pxid,
							obsusername: this.obsusername,
						},
						success(res) {
							console.log(res)
							if(res.length) {
								if(res[0].code == '0') {
									resolve(true)
								} else {
									resolve(false)
								}
								that.process = res[0]
							} else {
								uni.showToast({
									title: '无法获取流程数据',
									icon: 'none'
								})
							}
						}
					})
				})
			},
			confirm(v) {
				this.$emit('confirm',v)
			}
		}
	}
</script>

<style scoped lang="scss">
	
</style>
